package http;

import okhttp3.*;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

public class HttpTest {

    public static void main(String[] args) {
        //testGet();
        String url = "https://api.iot.jiguang.cn/device/v1/baseinfo" + "?" + "device_name=dfd52468443e11bd";
        System.out.println("resp:"+doJGet(url,Utils.BASE64_AUTHORIZATION));
    }
    /**
     *@Author: caoxiabo on 2019/12/18 8:08
     *@Description: 极光IOT 设备基本信息获取
     */
    private static void testGet(){
        String device_name = "dfd52468443e11bd";
        OkHttpClient client = new OkHttpClient.Builder().build();
        Request request1 = new Request.Builder()
                .url("https://api.iot.jiguang.cn/device/v1/baseinfo" + "?" + "device_name="+device_name)
                .addHeader("Authorization", Utils.BASE64_AUTHORIZATION)
                .build();
        try {
            Response response = client.newCall(request1).execute();
            System.out.println("baseInfo:"+response.body().string());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String doJGet(String url,String auth){
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization",auth)
                .build();
        return  execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:38
     *@Description: get api
     */
    private static String doGet(String url){
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
        return  execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:37
     *@Description: post api
     */
    private static String doPost(String url,String reqbody,Map<String,Object> map){
        FormBody body=addParamToBuilder(reqbody, map);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        return execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:36
     *@Description: put api
     */
    private static String doPut(String url,String reqbody,Map<String,Object> map){
        FormBody body=addParamToBuilder(reqbody, map);
        Request request = new Request.Builder()
                .url(url)
                .put(body)
                .build();
        return execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:35
     *@Description: 删除 api
     */
    private static String doDelete(String url,String reqbody,Map<String,Object> map){
        FormBody body=addParamToBuilder(reqbody, map);
        Request request = new Request.Builder()
                .url(url)
                .delete(body)
                .build();
        return execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:22
     *@Description: 拼接请求参数
     */
    private static FormBody addParamToBuilder(String reqbody, Map<String,Object> map){
        FormBody.Builder builder = new FormBody.Builder();
        if(!StringUtils.isEmpty(reqbody)){
            if(reqbody.startsWith("?")){
                reqbody=reqbody.substring(1);
            }
            String[] params=reqbody.split("&");
            for(int i=0;i<params.length;i++){
                if(params[i].equals("")){
                    continue;
                }
                String [] kv=params[i].split("=");
                builder.add(kv[0], kv[1]);
            }
        }

        if(map!=null){
            Iterator<Map.Entry<String,Object>> ite= map.entrySet().iterator();
            for(;ite.hasNext();){
                Map.Entry<String,Object> kv=ite.next();
                builder.add(kv.getKey(), kv.getValue().toString());
            }
        }
        return builder.build();
    }
    /**
     *@Author: caoxiabo on 2019/12/18 8:22
     *@Description: 执行请求
     */
    private static String execute(Request request){
        try {
            OkHttpClient client = new OkHttpClient();
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "{\"error\":\"fail\"}";
    }

}
